package com.shoe.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;

import com.shoe.pojo.ShoeOrder;
import com.shoe.service.ShoeOrderService;
import com.shoe.service.UserLogisticService;
import com.shoe.util.AppUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

@Controller
@CrossOrigin
public class ReturnController {

    @Autowired
    ShoeOrderService shoeOrderService;

    @Autowired
    UserLogisticService userLogisticService;

    @RequestMapping("/getreturn")
    public String getReturn(HttpServletRequest request, HttpServletResponse response) throws AlipayApiException, IOException {
        //获取支付宝GET过来反馈信息
        Map<String,String> params = new HashMap<String,String>();
        Map<String,String[]> requestParams = request.getParameterMap();
        Iterator<String> iter = requestParams.keySet().iterator();
        while(iter.hasNext()){
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用
            //valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8");
            params.put(name, valueStr);
        }
        //RSA2验证
        boolean signVerified = AlipaySignature.rsaCheckV1(params, AppUtil.alipay_public_key, AppUtil.charset, AppUtil.sign_type); //调用SDK验证签名
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();

        //——请在这里编写您的程序（以下代码仅作参考）——
        if(signVerified) {
            //商户订单号
            String out_trade_no = request.getParameter("out_trade_no");
            ShoeOrder shoeOrder = new ShoeOrder();
            shoeOrder.setOrderNo(out_trade_no);
            shoeOrder.setOrderState("未发货");
            shoeOrderService.updataDate(shoeOrder);

            ShoeOrder shoeOrder2 = new ShoeOrder();
            shoeOrder2.setOrderNo(out_trade_no);
            List<ShoeOrder> list = shoeOrderService.selectByNo2(shoeOrder2);
            List<Long> list1 = new ArrayList<>();
            for (int i =0 ;i<list.size();i++){
                list1.add(list.get(i).getOrderId());
            }
            userLogisticService.addOrderIdList(list1);



            //支付宝交易号
            String trade_no = request.getParameter("trade_no");
            //付款金额
            String total_amount = request.getParameter("total_amount");

            return "redirect:http://localhost:63343/qiguai/cart/cart.html";


        }else {
            return "redirect:http://localhost:63343/qiguai/html/main.html";
        }
    }
}
